﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Shipwire.Messages
{
    [XmlRootAttribute("TrackingUpdateResponse")]
    public class TrackingResponse : ShipwireResponse
    {
        public string Status { get; set; }
        [XmlElement("Order")]
        public TrackingOrder[] Orders { get; set; }

        public int TotalOrders { get; set; }
        public int TotalShippedOrders { get; set; }
        public int TotalProducts { get; set; }
        public string Bookmark { get; set; }
    }

    public class TrackingOrder
    {
        protected const string ShipwireDateFormat = "yyyy-MM-dd hh:mm:ss";

        [XmlAttribute("id")]
        public string Id { get; set; }
        [XmlAttribute("shipped")]
        public string Shipped { get; set; }
        [XmlAttribute("shipper")]
        public string Shipper { get; set; }
        [XmlAttribute("shipperFullName")]
        public string ShipperFullName { get; set; }
        [XmlAttribute("shipDate")]
        public string ShipDate_SerializeHelper
        { 
            get
            {
                if(ShipDate.HasValue)
                    return ShipDate.Value.ToString(ShipwireDateFormat);
                return string.Empty;
            }
            set
            {
                DateTime date;
                if (DateTime.TryParse(value, out date))
                    ShipDate = date;
                else
                    ShipDate = null;
            }
        }
        [XmlAttribute("expectedDeliveryDate")]
        public string ExpectedDeliveryDate_SerializeHelper
        {
            get
            {
                if (ExpectedDeliveryDate.HasValue)
                    return ExpectedDeliveryDate.Value.ToString(ShipwireDateFormat);
                return string.Empty;
            }
            set
            {
                DateTime date;
                if (DateTime.TryParse(value, out date))
                    ExpectedDeliveryDate = date;
                else
                    ExpectedDeliveryDate = null;
            }
        }
        [XmlAttribute("handling")]
        public decimal Handling { get; set; }
        [XmlAttribute("shipping")]
        public decimal Shipping { get; set; }
        [XmlAttribute("packaging")]
        public decimal Packaging { get; set; }
        [XmlAttribute("insurance")]
        public decimal Insurance { get; set; }
        [XmlAttribute("total")]
        public decimal Total { get; set; }
        [XmlAttribute("returned")]
        public string Returned { get; set; }
        [XmlAttribute("returnDate")]
        public string ReturnDate_SerializeHelper
        {
            get
            {
                if (ReturnDate.HasValue)
                    return ReturnDate.Value.ToString(ShipwireDateFormat);
                return string.Empty;
            }
            set
            {
                DateTime date;
                if (DateTime.TryParse(value, out date))
                    ReturnDate = date;
                else
                    ReturnDate = null;
            }
        }
        [XmlAttribute("returnCondition")]
        public string ReturnCondition { get; set; }
        [XmlAttribute("href")]
        public string Link { get; set; }
        [XmlAttribute("affiliateStatus")]
        public string AffiliateStatus { get; set; }
        [XmlAttribute("manuallyEdited")]
        public string ManuallyEdited { get; set; }

        public TrackingNumber TrackingNumber { get; set; }

        [XmlIgnore]
        public DateTime? ShipDate { get; set; }
        [XmlIgnore]
        public DateTime? ExpectedDeliveryDate { get; set; }
        [XmlIgnore]
        public DateTime? ReturnDate { get; set; }
    }


    public class TrackingNumber
    {
        [XmlAttribute("carrier")]
        public string Carrier { get; set; }
        [XmlAttribute("href")]
        public string Link { get; set; }

        [XmlText]
        public string Value;
    }
}
